<?php
namespace app\api\service;
use think\Controller;
use think\Db;

class Shop extends Controller {
    static protected $err_code = 0;
    //申请成为店铺接口
    static public function shop_create($param) {
        if( !isset($param['member_id']) || !isset($param['shop_name'])
            || !isset($param['province']) || !isset($param['city']) || !isset($param['area']) || !isset($param['street'])
            || !isset($param['address']) || !isset($param['lat'])  || !isset($param['lon']) || !isset($param['proposer']) || !isset($param['card_no'])
            || !isset($param['id_card_front']) || !isset($param['id_card_in_hand']) || !isset($param['id_card_verso']) || !isset($param['business_license'])
            || !isset($param['image'])
        ){
            return ['data'=>'','code'=>400001,'msg'=> 'FAULT'];
        }

        /*调用验证器 验证数据*/
        $validate = validate('Shop');
        //判断是否可以申请

        if (!$validate->check($param)) {
            return ['data'=>'','code'=>$validate->getError(),'msg'=> 'FAULT'];
        }

        $data = [
            'member_id' => $param['member_id']
            ,'shop_name' => $param['shop_name']
            ,'proposer' => $param['proposer']
            ,'card_no' => $param['card_no']
            ,'id_card_in_hand' => $param['id_card_in_hand']
            ,'id_card_front' => $param['id_card_front']
            ,'id_card_verso' => $param['id_card_verso']
            ,'business_license' => $param['business_license']
            ,'province' => $param['province']
            ,'city' => $param['city']
            ,'area' => $param['area']
            ,'street' => $param['street']
            ,'address' => $param['address']
            ,'lat' => $param['lat']
            ,'lon' => $param['lon']
            ,'image' => $param['image']
            ,'apply_time' => time()
        ];

        // 启动事务
        Db::startTrans();
        try{
            $insert_id = Db::table('mob_shop')->insertGetId($data);
            Db::commit();
            return ['data'=>['server_demand_id' => $insert_id],'code'=>200,'msg'=> 'SUCCESS'];
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            return ['code'=>self::$err_code, 'msg'=> $e->getMessage()];
        }
    }

    //审核店铺接口
    static public function shop_audit($param) {
        if( !isset($param['member_id']) || !isset($param['shop_id'])
            || !isset($param['auditor']) || !isset($param['audit_status'])
        ){
            return ['data'=>'','code'=>400001,'msg'=> 'FAULT'];
        }

        // 启动事务
        Db::startTrans();
        try{
            $data = [
                'auditor' => $param['auditor']
                ,'audit_status' => $param['audit_status']
                ,'audit_time' => time()
            ];
            Db::table('mob_shop')->where("id",$param['shop_id'])->update($data);
            Db::commit();
            return ['code'=>200,'msg'=> 'SUCCESS'];
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            return ['code'=>self::$err_code, 'msg'=> $e->getMessage()];
        }
    }

    //编辑店铺产品接口
    static public function save_shop_product($param) {
        if( !isset($param['member_id']) || !isset($param['shop_id']) || !isset($param['goods_id'])
            || !isset($param['price']) || !isset($param['content']) || !isset($param['is_donation'])
        ){
            return ['data'=>'','code'=>400001,'msg'=> 'FAULT'];
        }

        // 启动事务
        Db::startTrans();
        try{
            $data = [
                'goods_id' => $param['goods_id']
                ,'shop_id' => $param['shop_id']
                ,'price' => $param['price']
                ,'content' => $param['content']
                ,'is_donation' => $param['is_donation']
            ];

            //判断产品是否存在
            $where = [
                'goods_id' => $param['goods_id']
                ,'shop_id' => $param['shop_id']
            ];
            $rs = Db::table('mob_shop_product')->where($where)->select();
            if( $rs ){
                $rs_update = Db::table('mob_shop_product')->where('id',$rs["id"])->update($data);
                if($rs_update){
                    $insert_id = $rs["id"];
                }
            }else{
                $insert_id = Db::table('mob_shop_product')->insertGetId($data);
            }

            Db::commit();
            return ['data'=>['server_demand_id' => $insert_id],'code'=>200,'msg'=> 'SUCCESS'];
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            return ['code'=>self::$err_code, 'msg'=> $e->getMessage()];
        }
    }

    //添加店铺产品图片接口

    // 编辑 店铺产品图片接口  待定


}





